from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

from mlh.utils.exceptions import logger
from news.models import Channels, News, Comments



class NewsListSerializer(serializers.ModelSerializer):
    class Meta:
        model = News
        fields = '__all__'


class ChannelListSerializer(serializers.ModelSerializer):
    """分类列表序列化器"""
    class Meta:
        model = Channels
        fields = '__all__'


# class CommentsListSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Comments
#         fields = '__all__'
#
#
# class CommentsCreateSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Comments
#         fields = ('id', 'news_id', 'parent_id', 'user_id', 'create_time', 'content')


class NewsDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = News
        # comments_set = CommentsSerializer(many=True)
        # fields = ('id', 'title', 'user', 'create_time', 'news_content', 'comments_set')
        fields = ('id', 'title', 'user', 'create_time', 'news_content')

class CommentSubmitSerializer(ModelSerializer):

    class Meta:
        model = Comments
        fields = ('__all__')

    def validate_content(self, value):
        if value:
            return value

        raise serializers.ValidationError("评论不许为空")

    def validate_news_id(self, value):
        if not value:
            raise serializers.ValidationError("评论不许为空")

        try:
            n = News.objects.get(id=value)
        except News.DoesNotExist:
            logger.error("问题不存在")

        return value

